package net.xujialiang.XTestRPA.Controller.TenantManager;

import net.xujialiang.XTestRPA.Config.SpringSecurityConfig.SecurityUser;
import net.xujialiang.XTestRPA.Entity.Tenant.XRPATenant;
import net.xujialiang.XTestRPA.Enum.TenantOrderEnum;
import net.xujialiang.XTestRPA.Model.ResultData;
import net.xujialiang.XTestRPA.Service.Tenant.XRPATenantOrderService;
import net.xujialiang.XTestRPA.Service.Tenant.XRPATenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

import static net.xujialiang.XTestRPA.Enum.ReturnCode.RC999;

@RequestMapping("/api/tenant")
@RestController
public class TenantMgr {
    @Autowired
    XRPATenantService tenantService;
    @Autowired
    XRPATenantOrderService tenantOrderSvc;

    @RequestMapping(value = "/fetchTenantInfo", method = RequestMethod.POST)
    @ResponseBody
    public ResultData fetchTenantInfo(@RequestBody Map<String, Object> reqBody, HttpServletRequest request, Authentication auth) {
        SecurityUser sUser = (SecurityUser) auth.getPrincipal();
        String ttc = sUser.getTTC();
        try {
            XRPATenant ret = tenantService.fetchTenantInfo(ttc);
            return ResultData.success(ret);
        } catch (Exception e) {
            return ResultData.fail(RC999.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/updatePlan", method = RequestMethod.POST)
    @ResponseBody
    public ResultData updatePlan(@RequestBody Map<String, Object> reqBody, HttpServletRequest request, Authentication auth) {
        String plan = (String) reqBody.get("plan");
        SecurityUser sUser = (SecurityUser) auth.getPrincipal();
        String ttc = sUser.getTTC();
        try {
            // 判断是否是租户管理员
            if(tenantService.isTenantAdmin(ttc, sUser.getUsername())){
                tenantService.updatePlan(ttc, plan);
                return ResultData.success();
            }else{
                throw new Exception("非租户管理员, 无权操作");
            }
        }catch (Exception e){
            return ResultData.fail(RC999.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/redeem", method = RequestMethod.POST)
    @ResponseBody
    public ResultData redeem(@RequestBody Map<String, Object> reqBody, HttpServletRequest request, Authentication auth) {
        Integer points = (Integer) reqBody.get("points");
        SecurityUser sUser = (SecurityUser) auth.getPrincipal();
        String ttc = sUser.getTTC();
        try {
            // 判断是否是租户管理员
            if(tenantService.isTenantAdmin(ttc, sUser.getUsername())){
                tenantService.redeemPoint(ttc, points);
                tenantOrderSvc.createOrder(ttc, "消费", "-"+String.valueOf(points/100), TenantOrderEnum.RedeemRPAPoints.getMessage()+"," +points +"点");
                return ResultData.success();
            }else{
                throw new Exception("非租户管理员, 无权操作");
            }
        }catch (Exception e){
            return ResultData.fail(RC999.getCode(), e.getMessage());
        }
    }

}
